package com.lft.freshordering.controller;

import com.lft.freshordering.entity.Goods;
import com.lft.freshordering.entity.Stypes;
import com.lft.freshordering.response.R;
import com.lft.freshordering.response.StatusEnum;
import com.lft.freshordering.service.IStypesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author tyhxzy
 * @since 2024-12-02
 */
//我的代码
@RestController
@RequestMapping("/lft/stypes")
public class StypesController {
    @Autowired
    private IStypesService stypesService;

    //查询分类
    @RequestMapping("/selectAll")
    public R selectAll() {
        List<Stypes> stypes = stypesService.selectAll();
        System.out.println(stypes);
        return R.success(stypes);
    }

    //根据分类名字查询商品
    @RequestMapping("/selectByType/{id}")
    public R selectByType(@PathVariable Integer id) {
        return R.success(stypesService.selectByType(id));
    }

    //查询商品详情
    @RequestMapping("/selectByName/{name}")
    public R selectByName(@PathVariable String name) {
        return R.success(stypesService.selectByName(name));
    }

    //查询所有商品
    @RequestMapping("/suoyou")
    public R selectAllGoods() {
        return R.success(stypesService.selectAllGoods());
    }

    //模糊查询
    @PostMapping("/sou")
    public R sou(@RequestBody Map<String, Object> map) {
        String goodsName = (String) map.get("goodsName");
        // System.out.println(goodsName);
        return R.success(stypesService.sou(goodsName));
    }







    //查询所有分类
    @RequestMapping("/strpe")
    public R<List<Stypes>> strpes(){
        List<Stypes> stypes = stypesService.selectList();
        System.out.println(stypes+"---------------");
        if (stypes != null){
            return R.success(stypes);
        }
        return R.error(StatusEnum.SERVICE_ERROR);
    }

    //修改分类
    @RequestMapping("/update")
    public R<String> update(@RequestBody Stypes stypes){
        System.out.println(stypes+"------=-=-=");
        int result = stypesService.updatestypes(stypes);
        if (result>0){
            return R.success("修改成功");
        }else {
            return R.error(StatusEnum.SERVICE_ERROR);
        }
    }

    //删除分类
    @RequestMapping("/delete")
    public R<String> delete(Integer id){
        int i = stypesService.deletestypes(id);
        if (i>0){
            return R.success("删除成功");
        }else {
            return R.error(StatusEnum.SERVICE_ERROR);
        }
    }

    @RequestMapping("/stypesid")
    public R<List<Stypes>> stype(){
        List<Stypes> goods = stypesService.selectstypess();
        if (goods != null){
            return R.success( goods);
        }else {
            return R.error(StatusEnum.SERVICE_ERROR);
        }
    }

    //新增
    @RequestMapping("/add/{tname}")
    public R<String> add(@PathVariable String tname){
        int result = stypesService.insertstypes(tname);
        if (result>0){
            return R.success("添加成功");
        }else {
            return R.error(StatusEnum.SERVICE_ERROR);
        }
    }

    //根据id查询goods
    @RequestMapping("/goodsid/{id}")
    public R<List<Goods>> goods(@PathVariable Integer id){
        List<Goods> goods = stypesService.selectGoodsByCategoryId(id);
        if (goods != null){
            return R.success(goods);
        }else {
            return R.error(StatusEnum.SERVICE_ERROR);
        }
    }

}
